package com.example.teamwork3.Service;


import com.example.teamwork3.Mapper.KselectsubMapper;
import com.example.teamwork3.Model.Kselectsub;
import com.example.teamwork3.Model.KselectsubComparator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class KselectsubService {

    @Resource
    KselectsubMapper kselectsubMapper;


    public List<Kselectsub> KSelectAll()
    {
        Map<String,Integer> wishList = new HashMap<>();
        wishList.put("计算机科学与技术",122);
        wishList.put("软件工程",121);
        wishList.put("网络空间安全",121);
        wishList.put("网络工程",121);
        List<Kselectsub> army=kselectsubMapper.SelectKselectsubByArmy();//调取退伍军人的志愿信息
        for(int i=0;i<army.size();i++)
        {
            String[] wish = {army.get(i).getWish1(), army.get(i).getWish2(), army.get(i).getWish3(), army.get(i).getWish4()};
            for (String s : wish) {
                int wishNum = wishList.get(s);//获取该专业剩余的专业人数
                if (wishNum > 0) {
                    //调用接口更改其为该专业
                    kselectsubMapper.UpdateSubjectById(s,army.get(i).getId());
                    army.get(i).setSubject(s);
                    //然后填入优先级
                    kselectsubMapper.UpdateRuleidById("0",army.get(i).getId());
                    army.get(i).setRuleid(0);//退伍军人默认优先级为0
                    //专业剩余人数-1
                    wishList.put(s,wishNum-1);
                    break;
                }
            }
        }
        List<Kselectsub> student=kselectsubMapper.SelectKselectsub();
        KselectsubComparator kselectsubComparator= new KselectsubComparator();
        Collections.sort(student,kselectsubComparator);
        for(int i=0;i<student.size();i++) {
            String[] wish = {student.get(i).getWish1(), student.get(i).getWish2(), student.get(i).getWish3(), student.get(i).getWish4()};
            for (int j = 0; j < wish.length; j++) {
                int wishNum = wishList.get(wish[j]);//获取该专业的剩余数量
                if (wishNum > 0) {
                    //调用接口设置其为该专业
                    kselectsubMapper.UpdateSubjectById(wish[j],student.get(i).getId());
                    student.get(i).setSubject(wish[j]);
                    //然后填入优先级
                    kselectsubMapper.UpdateRuleidById(Integer.toString(j+1),student.get(i).getId());
                    student.get(i).setRuleid(j + 1);//优先级为第几志愿
                    wishList.put(wish[j],wishNum-1);
                    break;
                }

            }
        }
        army.addAll(student);
        return army;
    }

}
